-
-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tutorial for cross-compilation #160
Conversation
c9bb3fd
to
068416c
Compare
Deploying with Cloudflare Pages
|
068416c
to
365c398
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rough first pass.
`autoconf terminology <https://www.gnu.org/software/autoconf/manual/autoconf-2.69/html_node/Hosts-and-Cross_002dCompilation.html>`_. | ||
|
||
.. note:: macOS/Darwin is a special case, as not the whole OS is Open Source. | ||
It's only possible to cross-compile between ``aarch64-darwin`` and ``x86_64-darwin``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically only from x86_64-darwin
to aarch64-darwin
. Not sure if I'd mention this here though, as aarch64-darwin
is far from mature at this point and cross compilation is not working at all in any channel currently…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically only from x86_64-darwin to aarch64-darwin.
Is that because we're missing pkgsCross.x86_64-darwin
or something else?
Not sure if I'd mention this here though, as aarch64-darwin is far from mature at this point and cross compilation is not working at all in any channel currently…
Since the fix from @alyssais should work, the whole cross-compilation is not well supported but I'd like this tutorial to show that it's entirely feasible with enough maintainers. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that because we're missing pkgsCross.x86_64-darwin or something else?
I'm not sure as to why exactly, but I can confirm that nixpkgs can't even cross compile from x86_64-darwin to x86_64-darwin due to an evaluation problem very early in stdenv
.
the whole cross-compilation is not well supported but I'd like this tutorial to show that it's entirely feasible with enough maintainers
Still, aarch64-linux for example has had more time to mature, accumulate fixes etc. Maybe not mentioning aarch64-darwin saves someone a bit of frustration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about a disclaimer that we've just added support for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a disclaimer and move this further down.
as you'd then like to build a compiler on the build platform, compile code on the | ||
target plaform and run the final executable on the host platform. | ||
|
||
Since that's rarely needed, we'll treat target platform the same as the build. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just write that you ignore the platform for this tutorial. The default case of targetPlatform is the same as hostPlatform afaik.
Also interesting to note: On the outermost interface nixpkgs doesn't support targetPlatform
, it only accepts localSystem
(~ build) and crossSystem
(~ host) since cross compiling a cross compiler is a rare use case outside of stdenv bootstrapping.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to clarify since it took me quite some time to figure out why target is not important and explain why it can be ignored to save confusion. The user will eventually stumble upon it as they dive into cross-compilation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've thought about this yesterday and maybe it would actually help the tutorial to explain target completely. This would make it very easy to clear the following things up:
- Why are binutils, pkg-config and cc prefixed? What does the prefix mean? This is quite easy to answer if you explain the relation of the target prefix to the target platform
- What is
buildPackages
? Here having an understanding of target is nice:buildPackages
usually has build and host platform equivalent tolocalSystem
, but its target iscrossSystem
— having an understanding of target automatically explains how we bootstrap our cross package sets
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed! I don't think I know enough to really explain those, unfortunately.
----------------------------- | ||
|
||
The build platform is determined automatically by Nix | ||
as it can just guess it during the configure phase. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also set it by passing localSystem
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what cases is that really needed? In most cases guessing should be sufficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two main use cases would be remote build (if you have a builder with a different platform than your local machine) and testing evaluation of cross-related things by simulating being on another platform.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to mention that then in #157 tutorial (and consider developing nixpkgs out of scope here).
cc @zimbatm @zupo (for all the previous help with tutorials!) @Ericson2314 @matthewbauer @alyssais (for cross-compilation expertise) @otobrglez @boxofrox (for thumbs up on the tutorial issue) |
Apologies for a noob question, but I'm getting this:
Am I doing something wrong? |
@k-bx In
the |
f9922e9
to
a928ff1
Compare
Not at all, I added |
Aha! It worked now, thanks. Sorry for hijacking the thread, but I'm trying to build hello.c for my Raspberry Pi Zero. What should I put instead of UPDATE: ok, figured it's just |
https://b5839745.nix-dev.pages.dev/tutorials/cross-compilation#determining-the-host-platform is supposed to teach how to determine platform config. https://b5839745.nix-dev.pages.dev/tutorials/cross-compilation#choosing-the-host-platform-with-nix is supposed to teach how to pick the right attribute with that platform config in mind. Let me know where my explaining went south :) |
@domenkozar oh, I see. I was confused by the terminology, assuming that the host is the machine that does the building, and the "target" (or some other term) is my raspberryPi, since that command runs nix to determine the architecture, and I would assume you wouldn't need to install Nix on raspberry pi. Now I see that you would. |
Again apologies for spamming, just trying to get that raspberry pi zero hello world to work. Installing nix is not an option (
Am I not reading the tutorial correctly? Again, sorry for noob stuff. |
a928ff1
to
d8ca836
Compare
d8ca836
to
4604ab5
Compare
@k-bx thanks, this is really useful! I've added a sentence to hint how to get platform config if it's not possible to use Nix:
Unfortunately, there's no simple way to show us all attribute name -> platform config pairs. |
Aha! Interestingly,
That's why I thought
That is indeed one of the pain-points of Nix: you see the final spell, but it's hard to inspect what other keys and values are available. I hope to dive into Nix near time and see if I can help improve this situation. |
@k-bx The The topic of dynamic linking and dependencies should be mentioned in the tutorial as well. For the example it is probably easier just using static linking though. |
@sternenseemann thank you! May I just mention that adding
I've tried adding glibc, that didn't help. |
@k-bx Oh yeah, you need to use the |
@sternenseemann thank you! Changing I guess I finally have a starting point of how to get from here to a point where I can cross-compile my Rust project that is using some native libraries :) |
@k-bx Yeah, this is something we should take into consideration as well. There is some special casing to be done for rust possibly, I'm not quite sure. In general the way would be:
|
Co-authored-by: John Ericson <git@JohnEricson.me>
Thanks everyone for the feedback! I've included most of it in the last commit, please take another look! |
c4e37e2
to
665302f
Compare
The example for developer environment uses now a cross compiler that emits static binaries. Thanks for bringing this up! |
665302f
to
608df83
Compare
I'm going to merge this in a few hours to prevent perfect becoming the enemy of the good. Let me know if I missed some feedback and happy to address it further, but I might convert it into issues to keep things going. |
Fixes #148 #113
rendered